// 背景色片与任务分类颜色保持一致
export function formatterBackColor(item) {
  switch (item) {
    case 1:
      return 'var(--task-tag-bg-color-project)'
    case 2:
      return 'var(--task-tag-bg-color-dept)'
    case 3:
      return 'var(--task-tag-bg-color-temporary)'
    case 4:
      return 'var(--task-tag-bg-color-regular)'
    default:
      return ''
  }
}

// 跟状态颜色保持一致
export function formatterStatusColor(item) {
  let color = ''
  switch (item.status) {
    case '待领取':
      color = 'rgba(230, 162, 60, 1)'
      break
    case '待处理':
      color = 'rgba(242, 208, 157, 1)'
      break
    case '处理中':
      color = 'rgba(64, 158, 255, 1)'
      break
    case '待验收':
      color = 'rgba(159, 206, 255, 1)'
      break
    case '未完成':
      color = 'rgba(255, 77, 77, 1)'
      break
    case '延期未完成':
      color = 'rgba(204, 62, 62, 1)'
      break
    case '延期完成':
      color = 'rgba(255, 130, 130, 1)'
      break
    case '正常完成':
      color = 'rgba(82, 155, 46, 1)'
      break
  }
  return color
}

// 任务完成程度颜色
export function formatterStatusTypeColor(item) {
  let color = ''
  switch (item) {
    case 1:
      color = 'rgba(217,0,27)'
      break
    case 2:
      color = 'rgba(236,128,141)'
      break
    case 3:
      color = 'rgba(129,211,248)'
      break
    case 4:
      color = 'rgba(215,215,215)'
      break
  }
  return color
}

export function priorityColor(value) {
  switch (value) {
    case 1:
      return {
        color: 'var(--elp-color-danger)',
        backgroundColor: 'var(--elp-color-danger-light-7)',
      }
    case 2:
      return {
        color: 'var(--elp-color-warning)',
        backgroundColor: 'var(--elp-color-warning-light-7)',
      }
    case 3:
      return {
        color: 'var(--elp-color-primary)',
        backgroundColor: 'var(--elp-color-primary-light-7)',
      }
    case 4:
      return {
        color: 'var(--elp-color-info)',
        backgroundColor: 'var(--elp-color-info-light-7)',
      }
  }
}

export function taskTypeColor(value) {
  switch (value) {
    case 1:
      return {
        color: 'var(--elp-color-warning)',
        backgroundColor: 'var(--elp-color-warning-light-7)',
      }
    case 2:
      return {
        color: 'var(--elp-color-danger)',
        backgroundColor: 'var(--elp-color-danger-light-7)',
      }
    case 3:
      return {
        color: 'var(--elp-color-primary)',
        backgroundColor: 'var(--elp-color-primary-light-7)',
      }
    case 4:
      return {
        color: 'var(--elp-color-info)',
        backgroundColor: 'var(--elp-color-info-light-7)',
      }
    case 5:
      return {
        color: 'var(--elp-color-primary)',
        backgroundColor: 'var(--elp-color-primary-light-7)',
      }
  }
}

// 当前状态文字
export function formatterCurrentStatus(item) {
  let text = ''
  switch (item) {
    case 1:
      text = '未开始'
      break
    case 2:
      text = '处理中'
      break
    case 3:
      text = '已提交'
      break
    case 4:
      text = '完成'
      break
  }
  return text
}

// 项目中任务状态颜色区分
export function formatterColor(data) {
  let color = ''
  switch (data) {
    case 1:
      color = 'var(--elp-color-primary)'
      break
    case 2:
      color = 'var(--elp-color-warning)'
      break
    case 3:
      color = 'var(--elp-color-info)'
      break
    case 4:
      color = 'var(--elp-color-success)'
      break
  }
  return color
}

export function formatterDate(dayValue) {
  let year = new Date().getFullYear()
  let month = new Date().getMonth() + 1
  let day = new Date().getDate() + Number(dayValue)
  let h = new Date().getHours() //获取小时
  let m = new Date().getMinutes() //获取分钟
  let s = new Date().getSeconds() //获取秒钟
  return (
    year +
    '/' +
    formatTimeText(month) +
    '/' +
    formatTimeText(day) +
    ' ' +
    formatTimeText(h) +
    ':' +
    formatTimeText(m) +
    ':' +
    formatTimeText(s)
  )
}
// 定义用于格式化时间的函数
export function formatTimeText(time) {
  return time < 10 ? `0${time}` : `${time}`
}

// 日报状态
export function formatReportStatus(val, annotateFlag) {
  let form = {}
  switch (val) {
    case 1:
      form = { label: '正常', color: 'var(--task-default-color)', bgcolor: '' }
      break
    case 2:
      form = { label: '已补录', color: 'var(--task-underway-color)', bgcolor: '#E1ECFF' }
      break
    case 3:
      form = { label: '休息', color: 'rgba(168,171,178,1)' }
      break
    case 4:
      form = { label: '请假', color: '#14984E', bgcolor: '#D6EEE0' }
      break
    case 5:
      form = { label: '未填写', color: '#FFA112', bgcolor: '#FCF4EA' }
      break
    case 6:
      form = { label: '未补录', color: '#F62B14', bgcolor: '#FEF0F0' }
      if (annotateFlag === false) {
        form = { label: '不可补录', color: '#F62B14', bgcolor: '#FEF0F0' }
      }
      break
    case 7:
      form = { label: '加班', color: '#0E8AAD', bgcolor: '#18bae736' }
      break
  }
  return form
}

// 任务类型
export function formatWorkType(val) {
  let form = {}
  switch (val) {
    case '1':
      form = { label: '正常任务', color: 'rgba(64,158,255,1)' }
      break
    case '2':
      form = { label: '问题修改', color: 'rgba(230,162,60,1)' }
      break
    case '3':
      form = { label: 'bug处理', color: 'rgba(245,108,108,1)' }
      break
    case '4':
      form = { label: 'bug处理', color: 'rgba(144,147,153,1)' }
      break
  }
  return form
}
// 任务状态
export function formatWorkStatus(val) {
  let form = {}
  switch (val) {
    case 1:
      form = { label: '未开始', color: 'rgba(64,158,255,1)' }
      break
    case 2:
      form = { label: '进行中', color: 'rgba(230,162,60,1)' }
      break
    case 3:
      form = { label: 'QA', color: 'rgba(245,108,108,1)' }
      break
    case 4:
      form = { label: '已完成', color: 'rgba(144,147,153,1)' }
      break
    case 5:
      form = { label: '归档', color: 'rgba(144,147,153,1)' }
      break
  }
  return form
}

// 数据扁平化
export function flatten(arr, field) {
  return arr.reduce((result, item) => {
    return result.concat(item, Array.isArray(item[field]) ? flatten(item[field], field) : [])
  }, [])
}
// 数据转型
export function arrayToTree(items, field, parentField, childrenField) {
  let res = [] // 存放结果集
  let map = {}
  // 判断对象是否有某个属性
  let getHasOwnProperty = (obj, property) => Object.prototype.hasOwnProperty.call(obj, property)
  // 边做map存储，边找对应关系
  for (const i of items) {
    map[i[field]] = {
      ...i,
      [childrenField]: getHasOwnProperty(map, i[field]) ? map[i[field]][childrenField] : [],
    }
    const newItem = map[i[field]]
    if (i[parentField] === 0) {
      res.push(newItem)
    } else {
      if (!getHasOwnProperty(map, i[parentField])) {
        map[i[parentField]] = {
          itemsChildren: [],
        }
      }
      map[i[parentField]][childrenField].push(newItem)
    }
  }
  return res
}

// html转换纯文本
export function convertToPlain(html) {
  const regex = /<(?:[^>]+)>/g
  let res = html
  if (regex.test(html)) {
    const element = document.createElement('div')
    element.innerHTML = html
    res = element.textContent || element.innerText || ''
  }

  return res
}

// 文件限制
export function uploadAccept(arr = ['png', 'jpg', 'jpeg', 'pdf', 'doc', 'xls', 'xlsx', 'docx', 'mp4', 'avi']) {
  let res = arr.map((type) => '.' + type).join(',')
  return res
}

/**
 * 遍历树结构根据key找到某一属性
 * @param {object} tree
 * @param {number} targetId
 * @param {string} key
 */
export function treeFind(tree, targetId, key = 'id') {
  if (parseInt(tree[key]) === parseInt(targetId)) {
    return tree
  } else if (Array.isArray(tree.children)) {
    for (let i = 0; i < tree.children.length; i++) {
      const result = treeFind(tree.children[i], targetId, key)
      if (result !== null) {
        return result
      }
    }
  }
  return null
}

// 上午下午判断
export function timeJudgment(data, val) {
  // let res = data.find(item => item.dictLabel)
  // let form = {}
  // switch (val) {
  //   case '1':
  //     form = { label: '正常任务', color: 'rgba(64,158,255,1)' }
  //     break
  //   case '2':
  //     form = { label: '问题修改', color: 'rgba(230,162,60,1)' }
  //     break
  //   case '3':
  //     form = { label: 'bug处理', color: 'rgba(245,108,108,1)' }
  //     break
  //   case '4':
  //     form = { label: 'bug处理', color: 'rgba(144,147,153,1)' }
  //     break
  // }
  // return form
}

/**
 * 风险发生概率
 */
export function riskColor(val) {
  switch (val) {
    case 5:
      return {
        color: 'var(--elp-color-error)',
        backgroundColor: 'var(--elp-color-danger-light-7)',
      }
    case 4:
      return {
        color: 'var(--elp-color-danger)',
        backgroundColor: 'var(--elp-color-danger-light-7)',
      }
    case 3:
      return {
        color: 'var(--elp-color-primary)',
        backgroundColor: 'var(--elp-color-primary-light-7)',
      }
    case 2:
      return {
        color: 'var(--elp-color-primary)',
        backgroundColor: 'var(--elp-color-primary-light-7)',
      }
    case 1:
      return {
        color: 'var(--elp-color-info)',
        backgroundColor: 'var(--elp-color-info-light-7)',
      }
  }
}

/**
 * 风险严重程度
 */
export function riskSeverityColor(val) {
  switch (val) {
    case 1:
      return {
        color: 'var(--elp-color-error)',
        backgroundColor: 'var(--elp-color-danger-light-7)',
      }
    case 2:
      return {
        color: 'var(--elp-color-danger)',
        backgroundColor: 'var(--elp-color-danger-light-7)',
      }
    case 3:
      return {
        color: 'var(--elp-color-primary)',
        backgroundColor: 'var(--elp-color-primary-light-7)',
      }
    case 4:
      return {
        color: 'var(--elp-color-primary)',
        backgroundColor: 'var(--elp-color-primary-light-7)',
      }
    case 5:
      return {
        color: 'var(--elp-color-info)',
        backgroundColor: 'var(--elp-color-info-light-7)',
      }
  }
}
